﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using RentAWorker.Management.Infrastructure;
using RentAWorker.Desktop.Entities;
using RentAWorker.Business.Interfaces;
using System.Collections.ObjectModel;
using RentReturn;
using Microsoft.Practices.Composite.Regions;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using RentManagement;

namespace RentsList
{
    public class RentsListPresentationModel : IRentsListPresentationModel, INotifyPropertyChanged, IHeaderInfoProvider<string>,
                                                IClosingAware
    {
        private IUnityContainer Container { get; set; }

        #region Bound Properties

        private DateTime _fromDate = default(DateTime);
        public DateTime FromDate
        {
            get
            {
                return _fromDate;
            }
            set
            {
                _fromDate = value;
                OnPropertyChanged("FromDate");
            }
        }

        private DateTime _toDate = default(DateTime);
        public DateTime ToDate
        {
            get
            {
                return _toDate;
            }
            set
            {
                _toDate = value;
                OnPropertyChanged("ToDate");
            }
        }


        private Customer _selectedCustomer = default(Customer);
        public Customer SelectedCustomer
        {
            get
            {
                return _selectedCustomer;
            }
            set
            {
                _selectedCustomer = value;
                OnPropertyChanged("SelectedCustomer");
            }
        }

        #endregion

        private IRentRequestService RequestService { get; set; }
        private ICustomerService CustomerService { get; set; }

        public ICommand SearchCommand { get; set; }

        public RentsListPresentationModel(IUnityContainer container, IRentsListView view,
            ICustomerService customerSvc, IRentRequestService requestSvc)
        {
            this.Container = container;
            this.View = view;
            this.View.Model = this;

            this.RequestService = requestSvc;
            this.CustomerService = customerSvc;

            this.SearchCommand = new DelegateCommand<object>(LoadRentalServices);

            this.HeaderInfo = "Rentals";

            this.FromDate = DateTime.Now.Subtract(TimeSpan.FromDays(14));
            this.ToDate = DateTime.Now.AddDays(5);
        }

        #region IRentsListPresentationModel Members

        public IRentsListView View { get; set; }

        #endregion

        public void Initialize()
        {
            
        }

        public void Initialize(int customerID)
        {
            this.SelectedCustomer = this.CustomerService.GetCustomer(customerID);

            this.HeaderInfo = String.Format("Rentals for {0}", this.SelectedCustomer.Name);
        }

        private void LoadRentalServices(object nullParam)
        {
            var response = this.RequestService.GetRentalServicesForCustomer(
                new RentAWorker.Business.DTO.RentalServicesForCustomerRequest()
                {
                    CustomerId = this.SelectedCustomer.CustomerID,
                    FromDate = this.FromDate,
                    ToDate = this.ToDate
                }
            );

            if (response.Succeeded)
            {
                IRegionManager regionMgr = this.Container.Resolve<IRegionManager>();

                regionMgr.Regions["RentalList"].Clear();

                while (RentCommands.ConfirmAllCommand.RegisteredCommands.Count > 0)
                {
                    RentCommands.ConfirmAllCommand.UnregisterCommand
                        (RentCommands.ConfirmAllCommand.RegisteredCommands.First());
                }

                foreach (RentalService item in response.Services)
                {
                    IRentReturnPresentationModel pm = this.Container.Resolve<IRentReturnPresentationModel>();
                    pm.Initialize(item);

                    regionMgr.AddToRegion("RentalList", pm.View);
                }
            }
        }

        #region IHeaderInfoProvider
        private string _headerInfo = default(string);
        public string HeaderInfo
        {
            get
            {
                return _headerInfo;
            }
            set
            {
                _headerInfo = value;
                OnPropertyChanged("HeaderInfo");
            }
        } 
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        #region IClosingAware Members

        public bool RequestClose()
        {
            return this.CanClose;
        }

        public bool CanClose
        {
            get { return true; }
        }

        #endregion
    }
}
